/*
    hyh, version 1.0.0
    Copyright 20170704
    dec:公共函数
*/
;(function(hyh) {
    /**
     * [getDomain 获取域名]
     * 
     * @return {[string]} domain [域名]
     */
   hyh.getDomain = function() {
        var domain = document.domain;
        try {
            domain = domain.substring(domain.indexOf(".") + 1);
        } catch (e) {
            return "";
        }
        return domain;
    }
     /**
     * [setCookie 设置cookie]
     * @param {[string]} name     [关键字]
     * @param {[string]} objValue [保留的内容]
     */
    hyh.setCookie= function(c_name, value, expiredays, otherDomain) {
        // if (!otherDomain) {
        //     var myotherDomain = this.getDomain();
        // }
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        document.cookie = c_name + "=" + escape(value) +
        ((!expiredays)? "" : ";expires=" + exdate.toGMTString()) + ";path=/;"+ (otherDomain ? ("domain=" + otherDomain) : "");
    }
    /**
     * [getCookie 获取cookie]
     * @param {[string]} name     [关键字]
     * @return {[string]}      [description]
     */
    hyh.getCookie= function(name) { //取cookies函数       
        var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
        if (arr = document.cookie.match(reg)) return unescape(arr[2]);
        else return null;
    }
    /**
     * [delCookie 删除]
     * @param {[string]} name     [关键字]
     * @return {[type]}      [description]
     */
    hyh.delCookie= function(name, otherDomain) { //删除cookie
        // if (!otherDomain) otherDomain = this.getDomain();
        return document.cookie.indexOf(name) < 0 ? null : void(document.cookie = name + "=;expires=" + new Date(0).toGMTString() + ";path=/;"+ (otherDomain ? ("domain=" + otherDomain) : ""))
    }
    /**
     * [delAllCookies 删除所有cookie]
     * @return {[type]} [description]
     */
    hyh.delAllCookies= function() {
        var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
        if (keys) {
            for (var i = keys.length; i--;) {
                document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString() + ";path=/;domain=" + this.getDomain();
            }
            for (var i = keys.length; i--;) {
                document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString() + ";path=/";
            }
        }
    }
    
})(window.hyh ? window.hyh : window.hyh={}) 